package de.windeln.marketing.validation;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.google.common.base.Strings;

import de.windeln.marketing.campaign.model.RequestClientObject;

@Component
public class CostInformationOnlyByGivenDates implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == RequestClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		RequestClientObject rco = (RequestClientObject) target;
		if(rco.getFixCosts() != 0.0){
			if(rco.getCampaignStart() == null){
				errors.rejectValue("campaignStart", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
				errors.rejectValue("fixCosts", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
			}
			if(rco.getCampaignEnd() == null){
				errors.rejectValue("campaignEnd", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
				errors.rejectValue("fixCosts", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
			}
		}
		
		if(rco.getNumberOfVariable() != 0){
			if(rco.getCampaignStart() == null){
				errors.rejectValue("campaignStart", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
				errors.rejectValue("numberOfVariable", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
			}
			if(rco.getCampaignEnd() == null){
				errors.rejectValue("campaignEnd", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
				errors.rejectValue("numberOfVariable", "Cost Information only with Dates", "Dates have to be given to fill in cost information!");
			}
		}
		
	}

}
